import { BrowserWindow, app } from 'electron'
import { logger } from 'app/collect'
import { MessageBox } from 'app/dialog'

export function hackFakeCloseMainWindow(win: BrowserWindow) {
    win.on('close', (event) => {
        event.preventDefault()
        if (win.isFullScreen()) {
            win.setFullScreen(false)
        } else {
            win.hide()
        }
    })
}

export function onErrorMainWindow(win: BrowserWindow) {
    win.webContents.on('render-process-gone', async (event, details) => {
        const { response } = await MessageBox.question({
            title: '渲染器进程崩溃',
            message: `这个进程已经崩溃. ${details.reason}`,
            buttons: ['关闭', '重载'],
        })
        if (response === 0) {
            app.quit()
            return
        }
        win.reload()
    })
    win.webContents.on('console-message', (e: Event, level: number, message: string) => {
        const arr = ['debug', 'log', 'warn', 'error'] as const
        logger[arr[level] || 'log'](message)
    })
}
